package com.chen.authority.controller;

import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.MultiResponse;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.dto.RoleDto;
import com.chen.authority.model.qo.RoleQo;
import com.chen.authority.model.vo.RoleVo;
import com.chen.authority.service.RoleService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 角色管理
 *
 * @author chengy
 * @date 2022/11/21
 */
@Api(tags = "角色管理 RoleController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/role")
public class RoleController {

    private final RoleService roleService;

    /**
     * 新增角色
     *
     * @param dto 新增参数
     * @return Response
     */
    @ApiOperation("新增角色")
    @SaCheckPermission("system:role:add")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response add(@Valid @RequestBody RoleDto dto) {
        return roleService.addOrUpdateRole(null, dto);
    }

    /**
     * 删除角色
     *
     * @param ids 角色ids
     * @return Response
     */
    @ApiOperation("删除角色")
    @SaCheckPermission("system:role:delete")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Response delete(@PathVariable Integer[] ids) {
        return roleService.deleteRoleByIds(ids);
    }

    /**
     * 更新角色
     *
     * @param dto 更新参数
     * @return Response
     */
    @ApiOperation("更新角色")
    @SaCheckPermission("system:role:update")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public Response update(@PathVariable Integer id, @Valid @RequestBody RoleDto dto) {
        return roleService.addOrUpdateRole(id, dto);
    }

    /**
     * 获取角色详情
     *
     * @param id 角色id
     * @return SingleResponse<Role>
     */
    @ApiOperation("获取角色详情")
    @SaCheckPermission("system:role:update")
    @GetMapping("/{id}")
    public SingleResponse<RoleVo> get(@PathVariable Integer id) {
        return SingleResponse.success(roleService.getById(id).toVo(RoleVo.class));
    }

    /**
     * 查询角色列表
     *
     * @param pageQo 分页数据
     * @param roleQo 查询条件
     * @return PageResponse<Role>
     */
    @ApiOperation("查询角色列表")
    @SaCheckPermission("system:role:list")
    @GetMapping("/list")
    public PageResponse<RoleVo> list(PageQo pageQo, RoleQo roleQo) {
        return PageResponse.success(roleService.pageQueryRoleList(pageQo, roleQo));
    }

    @ApiOperation("导出角色列表")
    @SaCheckPermission("system:role:export")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoleQo qo) {
        ExcelUtil.exportExcel(response, "角色表", RoleVo.class, roleService.exportRoleList(WrapperUtil.buildQueryWrapper(qo)));
    }

    /**
     * 获取指定角色的菜单ids
     *
     * @param roleId 角色id
     * @return MultiResponse<Integer>
     */
    @GetMapping("/menu/{roleId}")
    @ApiOperation("获取指定角色的菜单id集合")
    @SaCheckPermission("system:role:update")
    public MultiResponse<Integer> findMenuIds(@PathVariable Integer roleId) {
        return MultiResponse.success(roleService.findMenuIdsByRoleId(roleId));
    }

}